package com.blic;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@MapperScan(basePackages = "com.blic.mapper")
public class SystemApplication {
	
	// 启动的时候要实例化RestTemplate类的一个实例
		@Autowired
		private RestTemplateBuilder builder;

		@Bean
		public RestTemplate restTemplate() {
			// 实例化RestTemplate模版
			return builder.build();
		}

		/**
		 * Spring Boot 服务跨域处理
		 * 
		 * @return WebMvcConfigurer
		 */
		@Bean
		public WebMvcConfigurer corsConfigurer() {
			return new WebMvcConfigurerAdapter() {
				// 默认方法：POST, GET, OPTIONS, DELETE, PUT；但系统只允许支持POST,GET
				@Override
				public void addCorsMappings(CorsRegistry registry) {
					registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST").allowedHeaders("*")
							.exposedHeaders("access-control-allow-headers", "access-control-allow-methods",
									"access-control-allow-origin", "access-control-max-age", "X-Frame-Options")
							.allowCredentials(false).maxAge(3600);
				}
			};
		}

		
	/**
	 * main
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		SpringApplication.run(SystemApplication.class, args);
	}

}
